package it.wzm.lightweightlog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class LogUtil {
	
	static String dir= "/home/leonardo/tmp/lightweight/";
	
	static void backup(LightweightlogObject o) throws LightweightlogException {		
		try {
			FileOutputStream fs = new FileOutputStream(dir + File.separator + o.getName() + ".bak");
			ObjectOutputStream os = new ObjectOutputStream(fs);
			os.writeObject(o);
		} catch (Exception e) {
			throw new LightweightlogException("Cannot serialize: " + e.getMessage() );
		}		
	}
	
	
	static LightweightlogObject restore(String name) {
		System.out.println("deserializing:" + name);
		try {
			FileInputStream fis = new FileInputStream(dir + File.separator + name + ".bak");
			ObjectInputStream ois = new ObjectInputStream(fis);
			LightweightlogObject o= (LightweightlogObject)ois.readObject(); 
			ois.close();
			return o;
			} catch (Exception e) { e.printStackTrace(); return null;}
	}
	
	

}
